package com.trkj.service.wxy;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wdm.DefinitionDeptDao;
import com.trkj.dao.wxy.AdmissionDao;
import com.trkj.dao.wxy.StaffInformationDao;
import com.trkj.pojo.Admission;
import com.trkj.pojo.DefinitionDept;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.vo.wxy.AdmissionPageVo;


@Service
@Transactional(rollbackFor = Exception.class)
public class AdmissionService {
	@Autowired
	private AdmissionDao admissionDao;
	
	@Autowired
	private StaffInformationDao staffInformationDao;
	
	@Autowired
	private DefinitionDeptDao definitionDeptDao;
	
	/**
	 * 查询所有的记录
	 */
	public List<AdmissionPageVo> queryByAdmissionPage(int page,int rows) throws ServiceException{
		try {
			List<Object[]> list = admissionDao.queryByAdmission(new PageRequest(page-1, rows));
			List<AdmissionPageVo> listAdmissionPageVo = new ArrayList<>();
			if(list != null){
				for (Object[] objs : list) {
					Admission a = (Admission) objs[0];
					StaffInformation s = (StaffInformation) objs[1];
					
					AdmissionPageVo admissionPageVo = new AdmissionPageVo();
					admissionPageVo.setAdmId(a.getAdmId());
					admissionPageVo.setAdmRemark(a.getAdmRemark());
					admissionPageVo.setAdmDate(a.getAdmDate());
					admissionPageVo.setAdmPerson(a.getAdmPerson());
					
					admissionPageVo.setStainName(s.getStainName());
					
					
					listAdmissionPageVo.add(admissionPageVo);
				}
			}
			return listAdmissionPageVo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 获取总记录数
	 * @return
	 */
	public long admissionByPageCount(){
		return admissionDao.count();
		
	}
	
	/**
	 * 查询所有的记录题库
	 */
	public List<StaffInformation> findAll_staName(){
		return (List<StaffInformation>) staffInformationDao.findAll();
	}
	
	/**
	 * 获取总记录数
	 * @return
	 */
	public long empByPageCount(){
		return staffInformationDao.count();
		
	}
	
	/**
	 * 分页查询 
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<StaffInformation> queryByPage(int page, int rows){
		return staffInformationDao.queryByPage(new PageRequest(page - 1, rows));
	}

	/**
	 * 新增
	 * @param admission
	 * @param staffInformation
	 * @param definitionDept
	 * @throws ServiceException
	 */
	public void addAdmission(Admission admission,StaffInformation staffInformation,DefinitionDept definitionDept ) throws ServiceException{
		try {
			
			StaffInformation qu = staffInformationDao.findOne(staffInformation.getStainId());
			DefinitionDept df = definitionDeptDao.findOne(definitionDept.getDefideId());
			
			admission.setStaffInformation(qu);
			admission.setAdmDate(new Timestamp(new Date().getTime()));
			
			
			admissionDao.save(admission);
		
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param admId
	 * @throws ServiceException
	 */
	public void admission_delete(Long admId) throws ServiceException{
		try {
			admissionDao.delete(admId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	
	}

	/**
	 * 考试试卷编辑
	 * @param epId
	 * @param examPageVo
	 * @return
	 * @throws ServiceException
	 */
	public AdmissionPageVo updateAdmision(Long admId,AdmissionPageVo admissionPageVo) throws ServiceException{
		
		try {
			Admission admission = admissionDao.findOne(admId);
			StaffInformation staffInformation = staffInformationDao.findOne(admission.getStaffInformation().getStainId());
			admissionPageVo.setAdmId(admId);
			admissionPageVo.setAdmRemark(admission.getAdmRemark());
			admissionPageVo.setStainId(staffInformation.getStainId());
			admissionPageVo.setStainName(staffInformation.getStainName());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
		return admissionPageVo;
		
	}
	
	/**
	 * 考试试卷编辑之后保存
	 * @param epId
	 * @param examPageVo
	 * @throws ServiceException
	 */
	public void updateSavaAdmission(Long admId,AdmissionPageVo admissionPageVo) throws ServiceException{
		try {
			Admission admission = admissionDao.findOne(admId);
			StaffInformation staffInformation = staffInformationDao.findOne(admissionPageVo.getStainId());
			
			admission.setAdmId(admId);
			admission.setAdmRemark(admissionPageVo.getAdmRemark());
			admission.setAdmDate(new Timestamp(new Date().getTime()));
			admission.setAdmPerson(admissionPageVo.getAdmPerson());
			admission.setStaffInformation(staffInformation);
			
			admissionDao.save(admission);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
}
